package com.loveprogrammer.eternity.factory;

import com.alibaba.fastjson.JSONArray;
import com.loveprogrammer.eternity.factory.template.EquipmentTemplate;
import com.loveprogrammer.eternity.model.game.HeroWrapper;
import com.loveprogrammer.eternity.model.product.Equipment;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0
 * @description: 公共装备库
 * @author: eric
 * @date: 2023-02-23 09:38
 **/
public class CommonEquipmentFactory {

    private static List<Equipment> commonEquipments;

    public static void init(String buffer) throws IOException {
        commonEquipments = JSONArray.parseArray(buffer, Equipment.class);
    }

    public static void addCommonEquipment(Equipment equipment, HeroWrapper heroWrapper){
        getCommonEquipments().add(equipment);
        heroWrapper.getWarehouse().getEquipmentList().remove(equipment);
    }

    public static void obtainCommonEquipment(Equipment equipment, HeroWrapper heroWrapper){
        heroWrapper.getWarehouse().getEquipmentList().add(equipment);
        getCommonEquipments().remove(equipment);
    }

    // 装备 装备后自动获取到本地
    public static void wearCommonEquipment(Equipment equipment, HeroWrapper heroWrapper){
        heroWrapper.getWarehouse().getEquipmentList().add(equipment);
        getCommonEquipments().remove(equipment);
        String takeOffId = heroWrapper.getHero().wear(equipment, heroWrapper.getHero().getTemplate().getWeaponType());
        heroWrapper.takeOff(takeOffId);
    }

    public static List<Equipment> getCommonEquipments() {
        if(commonEquipments == null){
            commonEquipments = new ArrayList<>();
        }
        return commonEquipments;
    }

    public static void setCommonEquipments(List<Equipment> commonEquipments) {
        CommonEquipmentFactory.commonEquipments = commonEquipments;
    }
}
